This page is intended to provide a brief and high-level introduction to using R and Spectre. Additional educational material on using R and R Studio are available on many sites, including the RStudio education site or this R Spatial page.
Before getting started, see our getting started page for installation instructions for R and RStudio.
To interact with the R programming language, we recommend using RStudio.
Open RStudio, and you should see something similar to the following:
To get started, create a new .R file and save it
There are two important types of text commonly found in R scripts:
Comments Any line in R code that starts with a
# is considered a comment. These are not executed by
RStudio as R code, but rather are used as notes to the user.
This is a comment:
## Run the following line to find your current working directory
Executable code A line or segment of code can be run
and will return some form of result. In the example below, the
getwd() function will return the location of the current
working directory.
This is the code:
getwd()
When the code is run, the output may look something like this:
[1] "/Users/Tom/Desktop"
For this demo we will use the ‘iris’ dataset, which consists of measurements of 150 flowers. Each row represents one flower, and each column represents a different measurement of that flower.
To run code in RStudio, we can either enter code into the script and selectively run elements of the code (preferred), or we can enter it directly into the console and run the code. For each of the code-blocks below, copy the code into your new script, press save, and then highlight and press CMD/CTRL return to execute the code
Copy the following into your script, save, then highlight the code and press CMD/CTRL return. The first command we will run is to load the ‘iris’ dataset and save it as the object ‘dat’. The lines starting with ‘#’ are only comments, and will not excute as commands (even if you select them and press CMD + return).
## Part 1: read the dataset
# Use the 'iris' dataset (150 flowers one per row) with various measurement (each column is a different measurement)
dat <- iris
After executing, you should should see a new object in the workspace (top right). This will be called ‘dat’, containing 150 observations, and 5 variables.
Next we will review the dimensions of ‘dat’ (how many rows and columns) and preview data from the first 6 rows of dat.
Copy the following into your script, save, then highlight the code and press CMD/CTRL return.
# Determine the number of rows and columns in the dataset
dim(dat)
dat <- iris
You should now see the following in the console. Lines starting with ‘>’ denote the commands that were executed. Lines without ‘>’ are the output. As you can see below the request to show the dimensions of our dataset using dim(dat) has given us 150 rows and 5 columns.
[1] 150 5
Copy the following into your script, save, then highlight the code and press CMD/CTRL return.
# Examine the first few lines of dataset
head(dat)
You should now see the following in the console. Lines starting with ‘>’ denote the commands that were executed. Lines without ‘>’ are the output. The request to preview the first 6 rows of our data using head(dat) has shown us the contents of the first 6 rows.
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
by a) clicking on the line or b) highlighting the text, and press CMD + return (Mac) or CTRL + Enter (Windows). You can run a single line of code (clicking on or highlighting the line), or multiple lines of code (highlight multiple lines of code).
If you highlight the code and press CMD + return (Mac) or CTRL + Enter (Windows), the code will be run. You can highlight multiple lines of code to run all at once.